How to Skip already completed portion of If/then Macro Code

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
I have a workbook where I can click a cell on the first sheet and make a checkmark. Based on that checkmark it will go to a data sheet, copy the corresponding column, then paste it into a report sheet.

The problem that I'm having is that everytime I check off a cell, it runs the code from the beginning and recopies the stuff it already copied.

So if I click Name, Height, and Weight on the first sheet (A1,B1,C1).
It runs the macro for the first click on A1 and copies Name to the report sheet. Then runs it for the second click on B1 and because A1 is still checked , copies Name and Height to the report sheet, and then for the third click on C1 it does the same thing so my report sheet has Name, Name, Name, Height, Height, Weight on it.

How can I get it to not copy/paste the same column again. The code I'm working with follows:

Private Sub Worksheet_SelectionChange(ByVal target As Range)
If target.Column = 2 Then Exit Sub
If target.Row < 2 Then Exit Sub
If target.Row > 14 Then Exit Sub
If target.Count > 1 Then Exit Sub
If IsEmpty(target) Then
target.Formula = "=CHAR(252)"
target.Value = target.Value
With target.Font
.Name = "Wingdings"
.FontStyle = "Bold"
.Size = 8
End With
target.Borders.LineStyle = xlContinuous

Call TestClick

Else
target.ClearContents
End If
End Sub

Private Sub TestClick()
If Not IsEmpty(Range("a1")) Then
Builder "Name"
End If
If Not IsEmpty(Range("b1")) Then
Builder "Height"
End If
If Not IsEmpty(Range("c1")) Then
Builder "Weight"
End If

Sub Builder(MyHeader As String)
Dim MyColumn As Integer
With Worksheets("DATA")
MyColumn = .Rows(1).Find(What:=MyHeader, After:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
.Columns(MyColumn).Copy Worksheets("REPORT").Range("A1").End(xlToRight).Offset(0, 1)
End With
End Sub

Just in general, how can I alter this? And how can I put in code to skip portions of if/then criteria if they have already been completed. I thought about just putting in code to delete duplicate columns, but I have 300+ columns to check from so the report sheet rapidly fills up...
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Upon closer inspection...

It doesn't look like it is doing the selections in the selected order, but rather the order that they appear in the data sheet.

For example. If I select A1, B1, C1 for "1", "2" , "3".

I thought it was running the macro for the first one A1, so copy "1" and paste it in the report sheet. Then run the macro for the second one B1, so copy "1" again since it is still checked, and "2", then paste. Then run it again for the third seclection C1. So it would give me 1,1,2,1,2,3 in the report sheet.

I thought I could select A1,C1, B1 and it would paste it as 1,3,2.

However, it looks like regardless of how I select them, it always pastes them in the order they appear on the data sheet.

So I really need to boil down the code so that it only copies/pastes the NEWLY checked entry. That way it's only adding what I check off and so should add them in the order I indicate.
 
Upvote 0

Forum statistics

Threads
1,217,388
Messages
6,136,304
Members
450,002
Latest member
bybynhoc

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