IF / VBA help!

heman334

New Member
Joined
Jun 20, 2013
Messages
6
Hey, all i need to do is if column "A" has the number "1", "2", or "3", then select "B2 TO D2" cut it, paste at E1, THEN delete row 2. Etc....

See below:

My problem:
ABCD
11131700633xxxabc10
21
aaa1a1
31131700634xxxabc10
42
aaa2a2
51131700635xxxabc11
61
aaa3a3

<tbody>
</tbody>


What it should look like
ABCDEFG
11131700633
xxxabc10aaa1a1
21131700634xxxabc10aaa2a2
31131700635xxxabc11aaa3a3

<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
heman334,

Thanks for the workbook.

Sample raw data in worksheet raw (not all columns are shown for brevity):


Excel 2007
ABCDNOPQ
1Unit NumberLocationOperation TypeTimestampHousing pipe serial number (Barcode)Housing Set (serial number)Output Shim Size L.H. Calculated [mm]Output Shim Size R.H. Calculated [mm]
21131700628xxxa6/19/2013 7:01S12061353
35A0773113-360+286.000mm+ 1000N+308.001mm+ 26000N
41131700629xxxa6/19/2013 7:02S12061353
55A0773143-354+286.000mm+ 1000N+308.001mm+ 26000N
61131700630xxxa6/19/2013 7:03S12061353
75A0773153-373+286.000mm+ 1000N+308.001mm+ 26000N
82131700636xxxStation worked OK6/19/2013 7:07A00586430G3.253.65
92131700637xxxStation worked OK6/19/2013 7:07A00586530G3.253.6
102131700638xxxStation worked OK6/19/2013 7:08A00586630G3.23.7
1111703G0970xxxStation worked OK6/19/2013 11:17S12061361
125A0650923C2.955+292.000mm+ 1000N+312.001mm+ 34000N
135
1411703G0971xxxStation worked OK6/19/2013 11:17S12061361
155A0650863C2.9545+292.000mm+ 1000N+312.001mm+ 34000N
165
1711703G0972xxxStation worked OK6/19/2013 11:18S12061361
185A0650853C2.9524+292.000mm+ 1000N+312.001mm+ 34000N
195
20
raw


After the macro:


Excel 2007
ABCDNOPQ
1Unit NumberLocationOperation TypeTimestampHousing pipe serial number (Barcode)Housing Set (serial number)Output Shim Size L.H. Calculated [mm]Output Shim Size R.H. Calculated [mm]
21131700628xxxa6/19/2013 7:01S12061353A0773113-360
31131700629xxxa6/19/2013 7:02S12061353A0773143-354
41131700630xxxa6/19/2013 7:03S12061353A0773153-373
52131700636xxxStation worked OK6/19/2013 7:07A00586430G3.253.65
62131700637xxxStation worked OK6/19/2013 7:07A00586530G3.253.6
72131700638xxxStation worked OK6/19/2013 7:08A00586630G3.23.7
811703G0970xxxStation worked OK6/19/2013 11:17S12061361A0650923C2.955
911703G0971xxxStation worked OK6/19/2013 11:17S12061361A0650863C2.9545
1011703G0972xxxStation worked OK6/19/2013 11:18S12061361A0650853C2.9524
11
12
13
14
15
16
17
18
19
20
raw


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub ReorgData()
' hiker95, 06/21/2013
' http://www.mrexcel.com/forum/excel-questions/709737-if-visual-basic-applications-help.html
Dim r As Long, lr As Long, lc As Long, rng As Range
Application.ScreenUpdating = False
With Sheets("raw")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  For r = 2 To lr Step 1
    If .Cells(r, 3) >= 1 And .Cells(r, 3) < 4 Then
      lc = .Cells(r, .Columns.Count).End(xlToLeft).Column
      Set rng = .Range(.Cells(r, 2), .Cells(r, lc))
      rng.Copy Destination:=.Range("O" & r - 1)
      rng.ClearContents
    End If
  Next r
  On Error Resume Next
  .Range("B2:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Put this formula in E1 =IF(MOD(ROW(),2)=1,B2,"") then copy E1 to F1 and G1. Copy (E1:G1) until the end of your data set. Select column E:G copy paste special values, sort your data set (A:G) and clear blank cells.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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