VBA automatically adding up cells ish?

Ramballah

Active Member
Joined
Sep 25, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
Hello i have a question
I have this code i wrote for copying my charts to save the information once im done with. heres the code:
Code:
Sub Range_Copy_Example()    Dim First_Chart As String, Second_Chart As String, Third_Chart As String
    Dim Fourth_Chart As String
    First_Chart = Range("B2")
    Second_Chart = Range("K2")
    Third_Chart = Range("T2")
    Fourth_Chart = Range("B104")
    'This copies a cellrange and removes it.
    If First_Chart = "" = False And Second_Chart = "" = True Then
        Range("A1:H101").Copy Range("J1:Q101")
        Range("B2:D101").ClearContents
        Range("F2:H101").ClearContents
    ElseIf First_Chart = "" = False And Third_Chart = "" = True Then
        Range("A1:H101").Copy Range("S1:Z101")
        Range("B2:D101").ClearContents
        Range("F2:H101").ClearContents
    End If
End Sub
As you can see it does its job i want him to do. However i need more than 50 charts and this only covers the first 2 basically... Is there a way that VBA automatically adds up the amount of cells he needs to go to the right / down to paste the next chart? So i dont need to write this code 48 more times?? if im not clear enough just ask im willing to drop my file for download.
 
I think this will do what you want
It checks the value of the B2 eqivalent offset cell and if that is empty then paste the range there (as per your original code)
After pasting ONCE the sub is terminated but VBA must set calculations back to automatic
(otherwise could use Exit Sub instead of GoTo TheEnd

:eek: this assumes that the B2 equivalent cell is only empty until a range has been pasted there - after that it always contains a value
You originally had 2 conditions - do you also need that first condition included or is it enough to know that the B2 equivalent is empty?
If so then perhaps replace
Code:
            If cel.Offset(101 * d, 9 * i) = "" Then
with
Code:
            If cel <> "" And cel.Offset(101 * d, 9 * i) = "" Then

Code:
Sub CopyToCorrectRanges()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim rng1 As Range, rng2 As Range, rng3 As Range, [COLOR=#008080]cel As Range[/COLOR], d As Integer, i As Integer
    Dim myStr As String
    Set rng1 = Range("A1:H101")
    Set rng2 = Range("B2:D101")
    Set rng3 = Range("F2:H101")
    [COLOR=#008080]Set cel = Range("B2")[/COLOR]
    For d = 0 To 10
        For i = 0 To 4
            If d = 0 And i = 0 Then i = 1
            [COLOR=#ff0000]If cel.Offset(101 * d, 9 * i) = "" Then[/COLOR]
                rng1.Copy rng1.Offset(101 * d, 9 * i)
                rng2.ClearContents
                rng3.ClearContents
                GoTo [COLOR=#008080]TheEnd[/COLOR]
            [COLOR=#ff0000]End If[/COLOR]
        Next i
    Next d

[COLOR=#008080]TheEnd:[/COLOR]
Application.Calculation = xlCalculationAutomatic

End Sub
 
Last edited:
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Its more like with my condictions it was: if b2 = "" = false and offset = "" = true
So basically when the first chart is filled and second chart is empty then do it. But also. if chart 2 is filled now will it look for the 3th chart next time with that if? or will it keep looking for chart 2 if its empty or not?


EDIT* : Okay so i tested the code and it works how i want it to however. when im at 5 times going right basically. it wont go down. it will keep pasting the data in the 5th chart. aka removing the data that was there placing the new data. but not going down

EDIT AGAIN: ok wat. i cleared my sheet and now it works like a hot knife going through butter. its perfect. idk why it didnt work before... but holy **** its finally solved thanks alot man :d
 
Last edited:
Upvote 0
why it didnt work before?

- one subtle change was made to your code
Code:
For d = 0 To [COLOR=#ff0000]10[/COLOR]


thanks for the feedback (y)
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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