Use Value from inputbox as range

somers

New Member
Joined
May 12, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm trying to use the input from the user (Range or number that i can offset from a fixed cell).

The error returned is:
1654005555018.png


I Highlight the code where it goes wrong.
If i use Range("C16") it works
but if i use the variable (as range) afbnr i get the error.

What am i doing wrong here?

VBA Code:
Sub Uploadafbeelding1_C16()


    Dim afbnr As Range
      Set afbnr = Application.InputBox(prompt:="Selecteer cel voor afbeelding", Type:=8)
        If afbnr Is Nothing Then Exit Sub

    
    Dim profile As String
    On Error GoTo 0
    Dim fd As FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .Filters.Clear
        .Filters.Add "Picture Files", "*.bmp;*.jpg;*.gif;*.png"
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Title = "Selecteer afbeelding"
        .InitialView = msoFileDialogViewDetails
 
    If .Show = -1 Then
    
      Dim img As Shape
      Dim nH
      Dim nW
      Dim nPix
      Dim x
    
          Set img = ActiveSheet.Shapes.AddPicture _
                     (Filename:=fd.SelectedItems(1), _
                      LinkToFile:=msoFalse, _
                      SaveWithDocument:=msoCTrue, _
                      Left:=ActiveSheet.[B][COLOR=rgb(184, 49, 47)]afbnr[/COLOR][/B].Left + 2, _
                      Top:=ActiveSheet.Range("C16").Top + 2, _
                      Width:=-1, _
                      Height:=-1)
                      
               With img
                 .LockAspectRatio = msoTrue
               '  .Width = 38
                 .Height = 58
               End With
    
              With Selection
                    nH = img.Height + 4
                   ' nW = img.Width + 4
                    Range("C16").RowHeight = nH
                    'Range("C16").Rowwidth = nH
              End With
        
        Else
          MsgBox ("Geannuleerd.")
        End If
    
        End With
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
afbnr is a Range so your syntax is wrong here:
VBA Code:
ActiveSheet.afbnr.Left
(You cannot apply formatting inside VBA code tags. To apply formatting, use Rich code tags.)

Try this
VBA Code:
afbnr.Left

That should work but if that references the wrong sheet then this to force the ActiveSheet reference:
VBA Code:
ActiveSheet.Range(afbnr.Address).Left
 
Upvote 0
Solution
Now I want to add the selected picture to the same cell in two worksheets with the same row/column height & width adjustions.

I have folowing code, it works perfect in sheet 1 but in sheet 2 the picture is randomly pasted.

VBA Code:
Sub Uploadafbeelding1_C16()


    Dim afbnr As Range
      Set afbnr = Application.InputBox(prompt:="Selecteer cel voor afbeelding", Type:=8)
       If afbnr Is Nothing Then Exit Sub
       
afbnr.Copy
    
    On Error GoTo 0
    Dim fd As FileDialog

    

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .Filters.Clear
        .Filters.Add "Picture Files", "*.bmp;*.jpg;*.gif;*.png"
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Title = "Selecteer afbeelding"
        .InitialView = msoFileDialogViewDetails
     If .Show = -1 Then


      Dim img As Shape
      Dim nH
      Dim nW
      Dim nPix
      Dim x
      Dim I As Integer
      
For I = 1 To 2

          Set img = Worksheets(I).Shapes.AddPicture _
                     (Filename:=fd.SelectedItems(1), _
                      LinkToFile:=msoFalse, _
                      SaveWithDocument:=msoCTrue, _
                      Left:=afbnr.Left + 2, _
                      Top:=afbnr.Top + 2, _
                      Width:=-1, _
                      Height:=-1)
                                            
               With img
                 .LockAspectRatio = msoTrue
               '  .Width = 38
                 .Height = 58
               End With
    
              With Selection
                    nH = img.Height + 4
                   ' nW = img.Width + 4
                    afbnr.RowHeight = nH
                    'afbnr.Rowwidth = nH
              End With
              
Next I

        Else
          MsgBox ("Geannuleerd.")
          
                End If
           End With
End Sub

How do i make the range and row/column adjustments the same as in sheet 2 as in sheet 1?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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