Copy cell column range based on another cell value

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Hello again.

I have a sheet listing values on 5 separate columns in a helper column. Shown below. Depending on the cell value in N14, I would like to copy the range associated with the cell values in columns 1-5. For example, if the cell value in cell N14 is 5, I would like to copy range F15-F25 to another worksheet in the range B5-B15.

Cell Formulas
RangeFormula
C6:F6C6=LEFT(C2,LEN(C2)-SEARCH("-", C2))
C7:F7C7=RIGHT(C2,LEN(C2)-SEARCH("-", C2))
C8:F8C8=LEFT(C3,LEN(C3)-SEARCH("-", C3))
C9:F9C9=RIGHT(C3,LEN(C3)-SEARCH("-", C3))
C10:F10C10=LEFT(C4,LEN(C4)-SEARCH("-", C4))
C11:F11C11=RIGHT(C4,LEN(C4)-SEARCH("-", C4))
C13:F13C13=B13+1
C14:F14C14=LEN(C2)
C15:F25C15=WORKSHEET!H5-WORKSHEET!G5


END-OF-MONTH-PROCESSES.xlsm
B
5309.22
6-40.13
772.07
862.81
9-16.50
10172.49
1121.11
12422.46
13233.08
147.88
15-8.57
WORKSHEET
Cell Formulas
RangeFormula
B5:B15B5=K5-J5
Cells with Conditional Formatting
CellConditionCell FormatStop If True
1:1048576Cellcontains an errortextNO


Thanks for looking!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Have a try with this macro. Be aware that B15 of copied area in column 1 overlaps B15 of paste area B5:B15.
VBA Code:
Option Explicit
Sub CopyByCriteria()
    Dim col
    col = Range("N14").Value + 1                  'detect value in N14 and adjust number to columns 1-5 (B-F)
    Range(Cells(15, col), Cells(25, col)).Copy    'copy area from your chosen column
    Range("B5").PasteSpecial Paste:=xlPasteValues 'paste values starting from B5 and down to B15
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Have a try with this macro. Be aware that B15 of copied area in column 1 overlaps B15 of paste area B5:B15.
VBA Code:
Option Explicit
Sub CopyByCriteria()
    Dim col
    col = Range("N14").Value + 1                  'detect value in N14 and adjust number to columns 1-5 (B-F)
    Range(Cells(15, col), Cells(25, col)).Copy    'copy area from your chosen column
    Range("B5").PasteSpecial Paste:=xlPasteValues 'paste values starting from B5 and down to B15
    Application.CutCopyMode = False
End Sub
Ho rollis13. That's a nifty little snippet. It doesn't fit exactly my application,(not your fault) but it looks like it can be modified to fit. I ended up recording a copy/paste macro with about 50 lines of code to get it to do what i wanted. I knew there was something a lot easier. If I get it to work I'll report back the final code.

Thanks a bunch.
 
Upvote 0
A little tweaking and it executed perfectly! Thank you for the concise code. I learned something from your post and really appreciate it.
 
Upvote 0
Glad having been of some help (y).
Even for sharing reasons, I wouldn't mind taking a look at your final macro just to figure out what I didn't 'guess' from your first post.
 
Upvote 0
Sure. I have 3 different entries to enter so I adjusted for that. Originally the data was on 1 sheet and the paste was on another. So I just modified the one sheet and linked to that on the other. The macro ended up looking like this:

Sub CopyByCriteria276()
Dim col
col = Range("N14").Value + 1 'detect value in N14 and adjust number to columns 1-5 (B-F)
Range(Cells(15, col), Cells(25, col)).Copy 'copy area from your chosen column
Range("N16").PasteSpecial Paste:=xlPasteValues 'paste values starting from B5 and down to B15
Application.CutCopyMode = False
End Sub

Sub CopyByCriteria397()
Dim col
col = Range("N14").Value + 1 'detect value in N14 and adjust number to columns 1-5 (B-F)
Range(Cells(32, col), Cells(42, col)).Copy 'copy area from your chosen column
Range("N29").PasteSpecial Paste:=xlPasteValues 'paste values starting from B5 and down to B15
Application.CutCopyMode = False
End Sub

Sub CopyByCriteria1829()
Dim col
col = Range("N14").Value + 1 'detect value in N14 and adjust number to columns 1-5 (B-F)
Range(Cells(49, col), Cells(59, col)).Copy 'copy area from your chosen column
Range("N42").PasteSpecial Paste:=xlPasteValues 'paste values starting from B5 and down to B15
Application.CutCopyMode = False
End Sub

And the execution code:
Sub PeriodDebitCredits()
Dim path As String
path = ThisWorkbook.path
Application.ScreenUpdating = False
On Error GoTo errorhandler2
UserForm1.Show
UnprotectMe
Sheets("helper").Select
CopyByCriteria276
CopyByCriteria397
CopyByCriteria1829

finish:
Sheets("worksheet").Select
ActiveSheet.Unprotect
Range("A1").Select
ActiveCell.FormulaR1C1 = "Debits/Credits Imported from Period:"
ProtectMe
ActiveWindow.SmallScroll Down:=-80
Range("f1").Select
ProtectMe
Exit Sub

errorhandler2:
MsgBox "ERROR"

End Sub

i know it's rudimentary but as long as it works right? I won't embarrass myself and show what I was using prior to your help, but it DID work.

Thanks again.
 
Upvote 0
Thank you.
I won't embarrass myself and show what I was using prior to your help, but it DID work.
It's okay, I believe you :eek::biggrin:.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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