Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

VBA to Copy range from Sheet to another and defined a name

0

Good day all

I need a vba code to do the following comands:

1 - copy range ("GT300") and ("GV300", Selection.End(xlToRight)) from ("Sheet1")
2 - H lookup ("Sheet1") ("GT291") on ("Sheet2").Range("B:B")
3 - Add the copied ranges from ("Sheet1") as new entery on ("Sheet2") based on the H lookup result
4 - Create new defined name from ("Sheet2") last entery from column D to last value on same row , Selection.CreateNames Top:=False, Left:=True, Bottom:=False, Right:=False

Answer
Discuss

Discussion

Hi Qaroos and welcome to the Forum 

We can answer you better if you help by providing a sample Excel file. Please edit your question and use the Add Files...button to attach a representative Excel file.
John_Ru (rep: 6152) Sep 29, '23 at 12:35 pm
Excel file added
Qaroos (rep: 4) Oct 8, '23 at 6:39 am
Add to Discussion

Answers

0
Selected Answer

Qaroos

In the FIRST attached revised file, the code below is assigned to your blue button (click it and it will run). Note that I cleared the data from sheet 2 (under WWW) so you can try it.

It uses one For/Next loop to find the text in cell GT291 on sheet 2 column B (and match the colour you used).

If found, a second loop looks for the first empty cell in B then copies values from Sheet 1 GT300:HN300 to that row. If the 8th row under that category is used, it warns the user.

It clears the sheet 1 data row, ready for the next new entry (or warns if GT291 isn't found for some reason).

The code is commented so you can follow and comment out bits you don't want (e.g. the one clearing copied data):

Sub RectangleRoundedCorners3_Click()

    Dim Opt As String

    ' caputre "category"
    Opt = Sheet1.Range("GT291").Value
    ' find category heading on sheet2
    For n = 3 To 33
        With Sheet2.Cells(n, 2)
            If .Value = Opt And .Interior.Color = 49407 Then
                'matched header and cell colour so loop down to find first blank row
                For p = 1 To 8
                    With .Offset(p, 0)
                        If .Value = "" Then
                           'column B blank so write values
                           .Resize(1, 21).Value = Sheet1.Range("GT300:HN300").Value
                           'warn user if necessary
                           If p = 8 Then MsgBox "Warning! Last row used for category " & Opt
                           Exit For
                        End If
                    End With
                Next p
                Exit For
            End If
        End With
    Next n

    If n < 34 Then
        'clear range and tell user
        Range("GT300:HN300").Value = ""
        MsgBox "Copied to sheet 2, row " & n + p

        Else
        MsgBox "Not copied, no matching category for " & Opt
    End If

End Sub

REVISION 23 November 2023

Further to the user's Discussion comments on Defined Names, it isn't clear what is required but my guess is included in the SECOND attached revised file. The bold portions are added (or changed) code to add a name for the added range:

Sub RectangleRoundedCorners3_Click()

    Dim Opt As String, n As Long, p As Long, DefNm As String

    ' capture "category"
    Opt = Sheet1.Range("GT291").Value
    ' find category heading on sheet2

    For n = 3 To 33
        With Sheet2.Cells(n, 2)
            If .Value = Opt And .Interior.Color = 49407 Then
                'matched header and cell colour so loop down to find first blank row
                For p = 1 To 8
                    With .Offset(p, 0)
                        If .Value = "" Then
                           'column B blank so write values
                           .Resize(1, 21).Value = Sheet1.Range("GT300:HN300").Value

                           ' define name , replacing any spaces with _ (undersore)
                           DefNm = Replace(Sheet1.Range("GV300").Value, " ", "_")
                           ' apply for added row, cells D:V
                           Sheet2.Names.Add DefNm, "=Sheet2!" & .Offset(0, 2).Resize(1, 19).Address

                           'warn user if necessary
                           If p = 8 Then MsgBox "Warning! Last row used for category " & Opt _
                                & vbCr & vbCr & "(You'll need to clear Defined Names to prevent muliple definitions)"
                           Exit For
                        End If
                    End With
                Next p
                Exit For
            End If
        End With
    Next n

    If n < 34 Then
        'clear NUMBERS range but leave formula in GV300 then tell user
        Range("GT300, GW300:HN300").Value = ""
        MsgBox "Copied to sheet 2, row " & n + p & " as Defined Name: " & DefNm

        Else
        MsgBox "Not copied, no matching category for " & Opt
    End If

End Sub

Note that I haven't included any checks on the existence of the new name to be created from the entry but the formula in cell GV300 is changed from:

=GT291&CONCAT(GX300:HS300)

to:

=If(CONCAT(GW300:HS300)<>"",GT291&CONCAT(GW300:HS300),"")

so it appears black without numbers entered but then appears as numbers are added to the (now) yellow cells GW300:HS:300. The formula is now retained once the entry has been made and the MessageBox reminds the user that, if that category is full, some defined names will need to be deleted.

As the file leaves me, the macro has already created a Defined Name of "WWW52081012345" (which refers to Sheet2!D4:V4) from an entry. I'm not sure why the user wants to use the names but that's not my concern!

Hope this fixes your problem. If so, please remember to mark this Answer as Selected

Discuss

Discussion

Perfect, the code is pretty good, 

Another question, is there a way to browse the rows on sheet2 from sheet1 in order if we need to update some input 
Qaroos (rep: 4) Oct 9, '23 at 8:35 am
Qaroos. I'm happy that the code is adequate (but, if so, the rules of the Forum indicate you should mark the Answer as Selected, not ask a supplementary question).. 

You could easily check just the copied row by selecting it, i.e. end the code (with chnages in bold):
    If n < 34 Then
        'clear range and tell user
        Range("GT300:HN300").Value = ""
        Sheet2.Activate
        Sheet2.Rows(n + p).Select
        MsgBox "Copied to sheet 2, row " & n + p
    
        Else
        MsgBox "Not copied, no matching category for " & Opt
    End If
 
End Sub


If that helps, good. If not (and say you want sheet2 data displayed in a userform or similar), please ask a new question and explain fully what you're trying to do.

Either way please remember to mark this Answer as Selected.
John_Ru (rep: 6152) Oct 9, '23 at 9:00 am
Qaroos. Disappointed that you gave no further response. You may have your own problems (hopefully not connected to current troubles in the Middle East) but once again I feel I've wasted my time helping you. 
John_Ru (rep: 6152) Oct 12, '23 at 6:01 am
hi john , sorry to late replay    your code is not complatly do what I ask for you may check on main Question there are 1- 2- 3- 4-   your code perfectly do 1- 2- 3- but not doing 4-   Creating new defined names and edit it, is main porpose of the code   and as what you say (I need by this code to displayed sheet2 defined names data in a userform) to update stored defined names, add new defined names or delete defined names if needed
Qaroos (rep: 4) Oct 13, '23 at 11:25 pm
Sorry Qaroos but I really don't know what you mean. I have no idea what form a new "Defined Name" might take (you give no clues) and you say "you may check on main Question there are 1- 2- 3- 4-" but I see no mention of such numbers.

If you make things clear (by editting your original question), I'll try to help. If not, I just can't.
John_Ru (rep: 6152) Oct 14, '23 at 8:29 am
Sorry again, as I told you previousely your code is perfect but it still not Create new defined name from the copied row.
Qaroos (rep: 4) Nov 23, '23 at 12:20 am
Qaroos. Thanks for selecting my Answer but you still haven't told me what sort of name you want. It can be added when a match is made in the loop starting:
                For p = 1 To 8
                    With .Offset(p, 0)

but I don't know what form the name should take (it must be unique, sa=tart with a letter or underscore). Please advise.
John_Ru (rep: 6152) Nov 23, '23 at 3:51 am
Okay, I made a guess! Please see my revised Answer (especially the text/code under REVISION 23 November 2023) and the SECOND attached file.
John_Ru (rep: 6152) Nov 23, '23 at 12:14 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login