copy and create problem

WERNER SLABBERT

Board Regular
Joined
Mar 3, 2009
Messages
107
i have a workbook,( great start to a stupid problem)
problem is my code doesn't code ... ( stating the obvious) what is suppose to happen is:
when i enter data into sheet 2 the data should be visible in sheet 1 as a Catalogue form, aswell as when i enter new data on the next availeble row of sheet 2 it should create a new " item" on sheet 1 with the same formatting as the previous "item" in addition to that it should paste a corresponding image from a set folder to the prescribed block that was created.

now my code doesn't want to do any of that... PLEASE HELP...
VBA Code:
Option Explicit ' Enforce explicit declaration

Sub CreateCatalogItem(ByVal Target As Range)
    On Error GoTo ErrHandler ' Enable error handling

    If Target.Worksheet.Name = "Sheet2" And Target.Row > 1 Then
        ' Check for data entry on Sheet2 (excluding header row)

        ' Find the next empty row on Sheet1
        Dim nextEmptyRow As Long ' More descriptive variable name
        nextEmptyRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1

        ' Copy data from Sheet2 to Sheet1, aligning with headers
        Sheets("Sheet1").Cells(nextEmptyRow, 4).Value = Target.Cells(1, 1).Value ' Description to D1
        Sheets("Sheet1").Cells(nextEmptyRow + 3, 4).Value = Target.Cells(1, 2).Value ' Used in to D4
        Sheets("Sheet1").Cells(nextEmptyRow + 4, 4).Value = Target.Cells(1, 3).Value ' Price to D5
        Sheets("Sheet1").Cells(nextEmptyRow + 5, 4).Value = Target.Cells(1, 4).Value ' Code to D6

        ' Insert and paste image into the designated box
        Sheets("Sheet1").Pictures.Insert (Target.Cells(1, 5).Value) ' Insert image
        Dim imageData As Variant ' More descriptive variable name
        imageData = Selection.ShapeRange.Picture
        ActiveSheet.Range("A1:B6").PasteSpecial ' Paste into A1:B6 (adjust if needed)
        ActiveSheet.Pictures(ActiveSheet.Pictures.Count).CopyData imageData

        ' Optional: Delete the original image shape if desired
        ' Selection.ShapeRange.Delete

        ' Merge cells for description and image block (as previously specified)
        ' ... (Code for merging cells) ...

        ' Optional: Wrap text in description cells
        Sheets("Sheet1").Range("D" & nextEmptyRow & ":H" & (nextEmptyRow + 2)).WrapText = True

        ' Format price cells as currency
        Sheets("Sheet1").Range("D" & (nextEmptyRow + 4)).NumberFormat = "R#,##0.00"
    End If

Exit Sub ' Exit before error handler if no errors occur

ErrHandler:
    MsgBox "An error occurred: " & Err.Description, vbCritical, "Error in CreateCatalogItem"
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    39 KB · Views: 14
  • Capture2.JPG
    Capture2.JPG
    80.2 KB · Views: 16

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,224,823
Messages
6,181,177
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