error 1004... moving data from first col to another (variable) column... complicated simple problem...

SnyperBunny

New Member
Joined
Jan 21, 2013
Messages
23
Hi guys, I have a column of data down my col B, and then more data staggered in various columns further to the right on my worksheet, I'm trying to write a 'simple' <.< *grrr* script to move the column over to the cell one to the left of my other data.

For example, if this is my current sheet: (the '......''s are essentially blank cells.

a.................................1
b.............................................2
c.....................................3
d.......................................................4
e...................................................................5
f...............................................6

I want it to look like this:

................................a1
..............................................b2
.....................................c3
.......................................................d4
....................................................................e5
.................................................f6


(note that the "a1", "b2", etc have nothing to do with row or column idicies in this example... I just used them as as easy way to demonstrate what I'm trying to do.)

I have the code below:

Code:
    t = 3
    k = 3
    
    Do While t < 65536                          'move the first col data to they should be
                                                
      If Cells(t, 1) <> "" Then                 'but also stop when you hit the bottom, or when you reach a blank row in col B.
        first_jan = ThisWorkbook.Worksheets(1).Cells(t, 2).Value
        
        If Cells(t, k) = "" Then                'checking that the Jan 2014 is not actually supposed to be Jan2014 with the rest of the column filled in.
            
            Do While Cells(t, k + 1) = ""       'while the cell col 'k+1'(the NEXT column) on the current row IS Nothing, increment k
                k = k + 1                       'will break once 'k+1' is equivalent to a cell NOT equal to nothing - therefore k is the cell that Jan2014 must be put into.
            Loop
                    
        End If
        
        ThisWorkbook.Worksheets(1).Cells(t, k) = first_jan
        
        ThisWorkbook.Worksheets(1).Cells(t, 2).Value = ""
        
      End If
      t = t + 1                                 'increment t for the next iteration.
    Loop

Prolem with is though, is that in amongst all the other giant code I have for this book, its just not doing what it is supposed to be doing... depending on what I tweek, it varies from doing nothing at all, to moving the data only one or two rows over.... never fully across to where it is supposed to be.

I tried to fiddle with a simpler version of it in a test workbook to get it working, but I am getting a random error that for the life of me I cannot figure out...
the error is: [Runtime Error 1004: Application defined or object defined error], on the line highlighted in yellow in the code below.
I tried Do Until <> "", I tried Do While = ""... Ugh I dont know what to try any more. :( Please can someone shed some light/wisdom/incredible smartness/experience on this issue...? There are imaginary cookies to be had!! :D

Code:
Sub test()
    Dim t As Integer
    Dim k As Integer
    Dim n As Integer
    
     
    t = 3
    k = 3
    n = k + 1
    Do While t < 20
                                                
              ThisWorkbook.Worksheets(1).Cells(t, 2).Value = "t = " & t
           
        If Cells(t, k) = "" Then
            
            [B][COLOR=#daa520]Do Until ThisWorkbook.Worksheets(1).Cells(t, k + 1).Value <> ""[/COLOR][/B]    [COLOR=#006400]'while the cell col 'k+1'(the NEXT column) on the current row IS Nothing, increment k
[/COLOR]              k = k + 1                      [COLOR=#006400] 'will break once 'k+1' is equivalent to a cell NOT equal to nothing
[/COLOR]           Loop
        
        End If
        
        ThisWorkbook.Worksheets(1).Cells(t, k) = "k = " & k
        t = t + 1
    Loop
    MsgBox ("done!")
End Sub
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This will do what you want, obviously the range involved is different - no clear what your range is.
Code:
Set Rng = Range("B1")

    While Rng.Value <> ""
          Rng.End(xlToRight).Offset(, -1) = Rng.Value
          Rng.ClearContents
          Set Rng = Rng.Offset(1)
    Wend
 
Upvote 0
Thank you for your help.
As for range, there IS no clear range. It needs to be able to handle any number of rows and columns, with the data each time being different, and also the location of the final data that is being moved.
 
Last edited:
Upvote 0
and this:


Code:
   Dim last_row as Range
   Set last_row = ThisWorkbook.Worksheets(1).Cells.Find("", Range("B2"), xlValues, xlWhole, xlByColumns, xlNext)
   Set Rng = Range("B3", last_row)

Did not work. wont compile - gives an error 1004 - method 'range' of object '_global' failed.


(and on another note - I'm still very new at vba coding, so if this is a silly little thing, please forgive my ignorance of it.)
 
Upvote 0
The code I posted will start in B1, which can be changed, and will go down the column until a blank cell is encountered.

It will move the value in column B across as many columns as required so it is in the column to the left of the next value in the row.

Isn't that what you want to do?
 
Upvote 0
Code:
   Dim last_row as Range
   Set last_row = ThisWorkbook.Worksheets(1).Cells.Find("", Range("B2"), xlValues, xlWhole, xlByColumns, xlNext)
   Set Rng = Range("B3", last_row)

Whose code is this?
 
Upvote 0
thats my attempt to find the location of the first blank in the B column.... All I could think of to find the blank so as to state a range.

and yes, I believe that is exactly what I want to do... however when I put your code in my macro... nothing happened... It compiled and ran, but no evidence of the code you posted being activated/doing anything...
 
Upvote 0
Did you try running the code I posted on its own?

Also, where does your data start in column B? Is it B1? If it isn't did you change the code to reflect that.

If you want to find the last row in column B and loop from row 3 down to it you can use this.
Code:
    LastRow = Range("B" & Rows.Count).End(xlUp).Row

    For I = 3 To LastRow
        With Range("B" & I)
            .End(xlToRight).Offset(, -1) = .Value
            .ClearContents
        End With
    Next I
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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