VBA Not selecting range properly

DaLiMan

Active Member
Joined
Jun 1, 2004
Messages
295
Hello,

I want to create a For/Next loop to select a range.
When I try out my code it does not select the way I want.

I expect to select only D6 and M6, but it selects D6 to M6 instead.
Code:
Worksheets("sheet1").Range("D" & 6, "M" & 6).Select

When I try this line it works fine, but cannot be used in a For/Next loop.
Code:
Worksheets("sheet1").Range("D6,M6").Select

Is anyone able to tell me what i'm doing wrong here?

Thanx,
Daniel
 
Yes it is. What's your current code look like?

Just figured out some code and a part of the code now looks something like this:
Code:
For I = 1 To Regels_deel100_ceiling
Call Upload_MaakNew(I)  ' create new workbook in other sub()
UploadBook = ActiveWorkbook.Name  'get the new name for later use.

Windows(WIKbook).Activate   ' switch back to the original workbook and use your Unio code.
    Union(Sheets("WIK data").Range("D" & ((100 * I) - 100) + 6).Resize(100), Sheets("WIK data").Range("M" & ((100 * I) - 100) + 6).Resize(100)).Copy
Windows(UploadBook).Activate  ' Back to new workbook 
ActiveSheet.Paste   'paste the selected items
Next

Now only have to save and close without anyone noticing, then a promt everything is ready for upload.
If you have any suggestion of tidy things up, it's very welcom!


PS: This "Union" thing is brilliant! Thanx again for that one...

Daniel
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Code:
Sub blah()
'Application.ScreenUpdating = False 'enable this line to hide activity
Set SourceSheet = ActiveSheet    'I'm assuming this is the active sheet when the macro reaches this point
With SourceSheet
    For i = 1 To Regels_deel100_ceiling
        Call Upload_MaakNew(i)  ' create new workbook in other sub()
        Set UploadBook = ActiveWorkbook  'get the new book for later use, but may not need this line at all.[COLOR=Red] (deleted in macro below)[/COLOR]
        Set UploadWS = ActiveSheet '[COLOR=Red]deleted in macro below[/COLOR]
        Union(.Range("D" & ((100 * i) - 100) + 6).Resize(100), .Range("M" & ((100 * i) - 100) + 6).Resize(100)).Copy [COLOR=Red]UploadWS.[/COLOR]Range("A1")
        ActiveWorkbook.Close True, "mynewfile " & i    'here I'm not sure if you've already given the file a name in Upload_MaakNew, so maybe just use: [COLOR=Blue].Close True[/COLOR]
    Next i
End With
Application.ScreenUpdating = True
End Sub
or
Code:
Sub blah2()
'Application.ScreenUpdating = False 'enable this line to hide activity
Set SourceSheet = ActiveSheet    'I'm assuming this is the active sheet when the macro reaches this point
With SourceSheet
    For i = 1 To Regels_deel100_ceiling
        Call Upload_MaakNew(i)  ' create new workbook in other sub()
        Union(.Range("D" & ((100 * i) - 100) + 6).Resize(100), .Range("M" & ((100 * i) - 100) + 6).Resize(100)).Copy Range("A1")    'this one assumes active sheet (new book) is destination sheet.
        ActiveWorkbook.Close True, "mynewfile " & i    'here I'm not sure if you've already given the file a name in Upload_MaakNew, so maybe just use: [COLOR=Blue].Close True[/COLOR]
    Next i
End With
Application.ScreenUpdating = True
End Sub
both untested. Some differences highlighted in red.
 
Upvote 0
both untested. Some differences highlighted in red.

Just tested both codes and they seem to work fine.
Had to alter Set UploadBook = ActiveWorkbook ==> into ==> Set UploadBK = ActiveWorkbook in the first code. Don't know why, but the first one gave an ERROR.

Think I'll go for the second code to spare some lines.... ;-)

Thanx a lot p45cal, I've learned a lot.
(altough I do not fully understand all of it.....LOL)
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top