VBA Code IF Not ISEMPTY, Copy/Paste

lamarh755

New Member
Joined
Jan 28, 2020
Messages
35
Office Version
  1. 2013
I have a spreadsheet where the data gets imported from a productivity database report. Some days the information comes across in alignment and other days the report columns are out of alignment. I already have a macro that aligns the data on days where the report comes across out of alignment. I am now trying to have a macro created to do the following...

* Set the range as ("D3:D") and the last used row in Column C
* For each cell in Column D that is not blank...
1) Copy the value in cell B of that same row and paste the data into cell A of that same row.
2) Copy the value in cell D of that same row and paste the data into cell B of that same row.

* Once that loop is complete, I want to clear the contents in Column D

Example below.

Thank you

VBA Capture 06.09.20.PNG
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

Try this with a copy of your data.
I have assumed that there will always be at least 1 non-blank cell in column D (with a corresponding blank cell in column A) & that those apparently blank cells in column A are in fact empty.

VBA Code:
Sub lamarh755()
  With Range("A3:A" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks)
    .Value = .Offset(, 1).Value
    .Offset(, 1).Value = .Offset(, 3).Value
  End With
  Columns("D").ClearContents
End Sub
 
Upvote 0
Thank you for providing the information about XL2BB. I will post the samples as advised going forward.

The code that you gave me worked, thank you. There will be times where there is no data at all in Column D. Whenever there is data in Column D, Column A will always be blank.
 
Upvote 0
There will be times where there is no data at all in Column D.
If that happens, then I assume that there would be no blanks in column A and my code would error. This would be safer:

VBA Code:
Sub lamarh755_v2()
  Dim rBlanks As Range
  
  On Error Resume Next
  Set rBlanks = Range("A3:A" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks)
  On Error GoTo 0
  If Not rBlanks Is Nothing Then
    With rBlanks
      .Value = .Offset(, 1).Value
      .Offset(, 1).Value = .Offset(, 3).Value
    End With
    Columns("D").ClearContents
  End If
End Sub
 
Upvote 0
I thought that the macro worked properly but it is actually not working properly. I downloaded the XL2BB add on and I have included a copy of the actual report I am working on. I originally abbreviated the range, thinking that I could adjust the code to the expanded range once I got the initial macro to work. I have been trying to figure this out on my own to no avail. With the expanded file, I am trying to have the code look for non blanks in Column T. When there are non blanks in Column T, copy the data from Column R and paste it into Column Q. Next, copy the data from Column T and paste it into Column R. The data originally showing in Column Q should remain there because when there is information in Column Q originally, Column T is blank. The last row changes on the report from day to day. Any assistance would be greatly appreciated. Thank you.

Book14
ABCDEFGHIJKLMNOPQRST
1Work TeamUserVarExpectedActualDirectIndirectDirect IIIndirect IIDirect IIIIndirect IIIPaid BreakTotalUnpaid BreakCountUnitsUnits/HrUnmeasured%Indirect%
2 Work Team: A SHIFT
3 D01671 59.128.690.000.001.890.000.001.0011.570.001351151324.98%18
4 D02041 1710.869.280.000.001.600.000.001.0011.880.002762072221.89%15
5 D02472 -99.4310.350.000.000.440.000.001.0011.790.001651581512.21%4
6 D02724 -168.6010.200.000.000.620.000.001.0011.830.001591531513.69%6
7 F00006 338.236.180.000.004.720.000.001.0011.900.002101722848.07%43
8 F06329 -501.623.220.000.007.460.000.001.0011.690.006925872.37%70
9 F07537 382.281.660.000.000.290.000.000.001.950.0026221314.87%15
10 F08980 3212.359.330.000.001.550.000.001.0011.880.002322242421.46%14
11 F10258 -334.086.040.000.004.950.000.001.0011.990.0017457949.62%45
12 F15490 -870.544.240.000.006.780.000.001.0012.020.004114364.73%62
13 F17060 -168.6610.360.000.000.430.000.001.0011.790.001661541512.13%4
14 F18072 1911.609.740.000.001.510.000.000.5011.750.002392212317.11%13
15 F18422 1313.1111.590.000.000.300.000.000.0011.880.00236227202.53%3
16 F19466 -310.4610.800.000.001.120.000.000.0011.910.00179172169.40%9
17 F19738 -221.582.030.000.000.030.000.000.002.060.003129141.46%2
18 F20106 -79.9110.670.000.000.240.000.001.0011.910.002051961810.41%2
19 F20899 7715.358.650.000.001.760.000.001.0011.400.00154146 Shipping Units 171724.21%
20 F21123 411.2010.760.000.000.170.000.001.0011.940.00187179179.80%2
21 F21124 -29.739.880.000.000.990.000.001.0011.870.00164156 Shipping Units 16916.76%
22 F21408 -267.379.960.000.000.700.000.001.0011.660.001521421414.58%7
23 F22531 -510.2810.780.000.000.630.000.000.5011.920.00191181179.48%6
24 F23421 2118.0714.890.000.000.090.000.000.5015.480.00419411283.81%1
25 F24222 -46.646.930.000.003.880.000.001.0011.810.001171051541.32%36
26 F24558 -62.632.790.000.004.160.000.001.007.950.006047 Shipping Units 176064.91%
27 F24825 -168.9110.570.000.000.340.000.001.0011.910.00139133 Shipping Units 13311.25%
28 F24829 -89.059.820.000.000.230.000.001.0011.060.001721651711.12%2
29 F26944 -246.949.140.000.001.650.000.001.0011.790.00119111 Shipping Units 121522.48%
30 F26946 -246.909.110.000.001.730.000.001.0011.840.00108102 Shipping Units 111623.06%
31 Work Team: B SHIFT  
32 F21806 1312.7411.260.000.000.260.000.000.0011.520.00224216192.26%2
33 F24237 1111.6710.540.000.000.370.000.001.0011.900.002152041911.51%3
34 F25031 -38.298.580.000.000.490.000.000.509.570.001311191410.34%5
35 F26941 0.000.000.000.005.940.000.000.506.440.00400100.00%100
36 Work Team: C SHIFT  
37 F10773 -125.396.140.000.001.270.000.000.507.910.001151001622.38%17
38 F18076 0.000.000.000.000.000.000.000.000.000.0020150 0
39 F22522 -89.4010.240.000.000.430.000.001.0011.670.001831421412.25%4
40 F24557 -681.414.330.000.006.680.000.001.0012.010.004226663.95%61
41 F25890 88.898.250.000.000.790.000.001.0010.040.001711632017.83%9
42 F26547 -37.127.340.000.001.690.000.001.0010.040.001271161626.79%19
43 Work Team: D SHIFT  
44 D30031 2411.819.510.000.000.080.000.000.009.590.00235223230.83%1
45 F00023 -98.869.780.000.000.440.000.000.0010.220.00142133144.31%4
46 F14722 -179.0010.890.000.000.030.000.001.0011.920.00134129128.64%0
47 F16868 -346.379.590.000.001.300.000.001.0011.890.00100941019.34%12
48 F22525 -79.9410.710.000.000.200.000.001.0011.920.002181991910.07%2
49 F23363 -158.7710.370.000.000.160.000.001.0011.530.001761711610.06%2
50 F23364 -138.9710.320.000.000.640.000.000.5011.460.00153141149.95%6
51 F23882 -277.6910.560.000.000.280.000.001.0011.840.001501441410.81%3
52 F23886 10.520.510.000.007.000.000.001.008.510.00169 Shipping Units 189394.01%
53 F23887 611.3510.680.000.000.770.000.000.5011.950.002342202110.63%7
54 F24236 010.9610.940.000.000.080.000.001.0012.010.00225195188.99%1
55 F24556 -258.4611.240.000.000.220.000.000.5011.960.00183178166.02%2
56 F25034 -23.823.880.000.001.290.000.000.505.680.0082741931.51%25
57 F25037 -99.8110.760.000.000.140.000.001.0011.900.00188183179.58%1
58 F25365 -550.741.670.000.000.820.000.000.002.490.002414 Shipping Units 83332.93%
59 F25367 -208.7210.850.000.000.050.000.001.0011.900.00153143138.82%0
60 F25370 -257.389.830.000.001.030.000.001.0011.870.001431341417.10%10
61 F25371 -139.2410.580.000.000.240.000.001.0011.820.001841751710.49%2
62 F25891 -158.9210.490.000.000.100.000.001.0011.600.00118113119.48%1
63 F26940 -683.5811.150.000.000.380.000.001.0012.530.0010095 Shipping Units 9311.01%
64 F26942 -640.762.090.000.002.550.000.000.505.150.001411 Shipping Units 55559.22%
65 F26951 5616.8010.740.000.000.080.000.001.0011.820.00148143 Shipping Units 1319.14%
66
67 Grand Total: -6472.84501.490.000.0086.070.000.0045.50633.060.0088778046161520.78%
Sheet1
Cell Formulas
RangeFormula
S67,S3:S65S3=IFERROR((F3+H3+J3+K3)/L3,"")
 
Upvote 0
OK, try this instead

VBA Code:
Sub lamarh755_v3()
  Dim rColQ As Range, rBlanks As Range
  
  Set rColQ = Range("Q3:Q" & Range("T" & Rows.Count).End(xlUp).Row)
  On Error Resume Next
  Set rBlanks = rColQ.SpecialCells(xlBlanks)
  On Error GoTo 0
  If Not rBlanks Is Nothing Then
    With rBlanks
      .FormulaR1C1 = "=if(RC[1]="""","""",RC[1])"
      rColQ.Value = rColQ.Value
      .Offset(, 1).FormulaR1C1 = "=if(RC[2]="""","""",RC[2])"
      rColQ.Offset(, 1).Value = rColQ.Offset(, 1).Value
      .Offset(, 3).ClearContents
    End With
  End If
End Sub
 
Upvote 0
That did exactly what I needed it to, thank you!!! I really appreciate your assistance.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,040
Members
449,063
Latest member
ak94

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