wiklendt
i recommend chocolate
- Local time
- Today, 10:46
- Joined
- Mar 10, 2008
- Messages
- 1,746
Hi everyone,
what i'm trying to do is manipulate excel data in preparation for import into access. i have progressed quite well, but i'm stuck on how to select a dynamic range from within access:
i have some columns of data, which i want to copy to a new worksheet. They are columns A:I, but non-fixed rows. There is superfluous data both above and below this range, so i can't use "last row" (at least, not in the sense that everyone else seems to be using it)
i have managed to find the start position of this data using:
in "more code here" i need to then select down the column (in a 'currentregion' fashion) and then across to the ninth column (I), so in essence i get a block selection from .Cells(i+2,1), down column A to the last cell before a blank cell break, then across to the I column. (see below):
(doesn't have to select, i just need to be able to copy it, but "select" lets me at least see visually that the code is working as it's supposed to while i'm testing).
once the range is found, i need to copy it to another new worksheet - this portion at least is working for me, but it's not finding the correct range to copy.
If i use:
then that works, but it's only one row.
i've tried using .Currentregion.Copy, but it only copies over the data from column A, so it appears that Currentregion is only good for the column of the active cell, and, if Excel Help is anything to go by, there can only be one active cell per region.
i thought myself terribly clever when i got the range address figured out with the following code:
which returns what i expect: "A24:I24"
but, either i have not declared it as the correct type, or i am not using the string correctly in my code (e.g. ".Range(strRange).Select"), because i get "Error 1004 - Application-defined or object-defined error". i get this error even if i leave it as an 'open' declaration (i.e., Dim strRange, without the "As String").
I tried to declare it as Range, but access didn't like that, so i tried ApXL.Range (i'd set my ApXL earlier), but access errors on that too - i've tried also declaring it as an object, with no luck.
to test my syntax, i used .Range("A24:I24").Select, i do not get an error, but again, i can't hardcode the range because it changes from file to file.
so i'm at my wits end (been at this for a week). Any suggestions mightily appreciated.
here are some more attempts of mine to select the correct range, commented with the associated error:
Attached is my full code for the function, which prompts user to select a file, adds a new worksheet and other bits and pieces directly related to what i want to do.
Here also is a sample xls file that needs this data manipulation done to it, in case anyone feels the urge to test stuff...
anyone need clarification on what i'm doing or why just let me know.
ta muchly for any assistance, guidance or tutorials on this sort of stuff!
what i'm trying to do is manipulate excel data in preparation for import into access. i have progressed quite well, but i'm stuck on how to select a dynamic range from within access:
i have some columns of data, which i want to copy to a new worksheet. They are columns A:I, but non-fixed rows. There is superfluous data both above and below this range, so i can't use "last row" (at least, not in the sense that everyone else seems to be using it)
i have managed to find the start position of this data using:
Code:
With xlWSh
.Activate
For i = 1 To .UsedRange.Rows.Count
If .Cells(i, 1) = "Isolate AST Results" Then
[i]...more code here[/i]
End If
Next
End With
in "more code here" i need to then select down the column (in a 'currentregion' fashion) and then across to the ninth column (I), so in essence i get a block selection from .Cells(i+2,1), down column A to the last cell before a blank cell break, then across to the I column. (see below):
(doesn't have to select, i just need to be able to copy it, but "select" lets me at least see visually that the code is working as it's supposed to while i'm testing).
once the range is found, i need to copy it to another new worksheet - this portion at least is working for me, but it's not finding the correct range to copy.
If i use:
Code:
.Range("A24:I24").Copy Destination:=xlWShExtract.Range("A2")
i've tried using .Currentregion.Copy, but it only copies over the data from column A, so it appears that Currentregion is only good for the column of the active cell, and, if Excel Help is anything to go by, there can only be one active cell per region.
i thought myself terribly clever when i got the range address figured out with the following code:
Code:
'find the address of the appropriate range
strRangeLeft = ApXL.Cells(i + 2, 1).Address(0, 0)
strRangeRight = ApXL.Cells(i + 2, 9).Address(0, 0)
strRange = Chr$(34) & strRangeLeft & ":" & strRangeRight & Chr$(34)
'returns A1 range reference - e.g., "A24:I24"
'MsgBox strRange
which returns what i expect: "A24:I24"
but, either i have not declared it as the correct type, or i am not using the string correctly in my code (e.g. ".Range(strRange).Select"), because i get "Error 1004 - Application-defined or object-defined error". i get this error even if i leave it as an 'open' declaration (i.e., Dim strRange, without the "As String").
I tried to declare it as Range, but access didn't like that, so i tried ApXL.Range (i'd set my ApXL earlier), but access errors on that too - i've tried also declaring it as an object, with no luck.
to test my syntax, i used .Range("A24:I24").Select, i do not get an error, but again, i can't hardcode the range because it changes from file to file.
so i'm at my wits end (been at this for a week). Any suggestions mightily appreciated.
here are some more attempts of mine to select the correct range, commented with the associated error:
Code:
' Copies only either column A data, or first row of block data.
'.Range(ApXL.Cells(i + 2, 1), ApXL.Cells(i + 2, 9)).CurrentRegion.Copy Destination:=xlWShExtract.Range("A2")
'.Range("A24:I24").Copy Destination:=xlWShExtract.Range("A2")
'' Error 1004 - Application-defined or object-defined error (does strRange need to be declared something different?
'.Range(strRange).Select
'' Error 438 - Object does not support this property or method
'.Range(ApXL.strRange).Select
'' Error 91 - object variable or with block variable not set
'.Range(Selection, Selection.End(xlDown)).Copy Destination:=xlWShExtract.Range("A2")
'.Range(ApXL.objRange).CurrentRegion.Copy Destination:=xlWShExtract.Range("A2")
' Does not error, but also copies only column A
'.Range(ApXL.Cells(i + 2, 1), ApXL.Cells(i + 2, 9)).CurrentRegion.Copy Destination:=xlWShExtract.Range("A2")
' The following worked to copy correct data from Column A - it's a start!
'.Range(ApXL.Selection, ApXL.Selection).CurrentRegion.Copy Destination:=xlWShExtract.Range("A2")
'' Error 1004 - Application-defined or object-defined error (does strRange need to be declared something different?
'.Range(ApXL.Selection, ApXL.Selection.End(xlDown)).Copy Destination:=xlWShExtract.Range("A2")
'---- Recorded Excel Macro doing various interesting things ----
'' CopyDeleteMoveColumns Macro
' Range("A24:I24").Select 'select the first antimicrobial and across to the Final SIR (9th col, 8 cols from A)
' Range(Selection, Selection.End(xlDown)).Copy Destination:=xlWShExtract.Range("A2") 'copy/paste to Extract!A2
'---------------------------------------------------------------
Attached is my full code for the function, which prompts user to select a file, adds a new worksheet and other bits and pieces directly related to what i want to do.
Here also is a sample xls file that needs this data manipulation done to it, in case anyone feels the urge to test stuff...
anyone need clarification on what i'm doing or why just let me know.
ta muchly for any assistance, guidance or tutorials on this sort of stuff!