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:
NameRelationshipImmediate FamilyNOK 1NOK 2NOK 3
FName LNameFatherSon - First Last
Mother - XX
Father - XY
First LastBrotherMother - XX
Father - XY

<tbody>
</tbody>

After VBA formatting:
NameRelationshipImmediate FamilyNOK 1NOK 2NOK 3
FName LNameFatherSon - First LastMother - XXFather - XY
First LastBrotherMother - XXFather - XY

<tbody>
</tbody>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,434
Office Version
  1. 365
Platform
  1. Windows
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.
 

butch3

Board Regular
Joined
Feb 4, 2019
Messages
54
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,434
Office Version
  1. 365
Platform
  1. Windows
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,601
Messages
5,838,311
Members
430,537
Latest member
Antonio11

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
Top