Variable not set up right?

butch3

Board Regular
Joined
Feb 4, 2019
Messages
54
Hi all. I've created this simple code to help me move some data around. I'm trying to move data from Column C to Column D, E, F, etc when there is no text in a cell within Column B. See my code and example below.

Sub Test()​

Dim rownum As Long​
Dim rowtext As Long​
Dim colmnltr As Long​

For rownum = 2 To 950​

If IsEmpty(Range("B" & rownum)) Then​

Range("C" & rownum).Select​
Selection.Cut​
colmnltr = colmnltr + 1​
Range(Columns(colmnltr) & rowtext).Select​
ActiveSheet.Paste​
Else​

rowtext = rownum​
colmnltr = 3​

End If​

Next rownum​

End Sub

Initial Data:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Relationship[/TD]
[TD]Immediate Family[/TD]
[TD]NOK 1[/TD]
[TD]NOK 2[/TD]
[TD]NOK 3[/TD]
[/TR]
[TR]
[TD]FName LName[/TD]
[TD]Father[/TD]
[TD]Son - First Last[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Mother - XX[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Father - XY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]First Last[/TD]
[TD]Brother[/TD]
[TD]Mother - XX[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Father - XY[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

After VBA formatting:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Relationship[/TD]
[TD]Immediate Family[/TD]
[TD]NOK 1[/TD]
[TD]NOK 2[/TD]
[TD]NOK 3[/TD]
[/TR]
[TR]
[TD]FName LName[/TD]
[TD]Father[/TD]
[TD]Son - First Last[/TD]
[TD]Mother - XX[/TD]
[TD]Father - XY[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]First Last[/TD]
[TD]Brother[/TD]
[TD]Mother - XX[/TD]
[TD]Father - XY[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about
Code:
Sub butch3()
   Dim rng As Range
   
   For Each rng In Range("B:B").SpecialCells(xlBlanks).Areas
      rng.Offset(-1, 2).Resize(1, rng.Count).Value = Application.Transpose(rng.Offset(, 1).Value)
      rng.Offset(, 1).Value = ""
     [COLOR=#ff0000] rng.EntireRow.Delete[/COLOR]
   Next rng
End Sub
If you don't want to delete the empty rows remove the line in red.
 
Upvote 0
That was incredibly efficient.

Is there any reason that my code didn't or wouldn't work? I guess I need to read more about the specialcells, areas, offset, resize, and transpose functions.
 
Upvote 0
To be honest I didn't actually look at your code.
But this wont work
Code:
Range(Columns(colmnltr) & rowtext).Select
as Range needs a letter for the column not a number.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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