Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA to determine column letter of currently selected cell

Posted on 2014-03-19
6
Medium Priority
?
651 Views
Last Modified: 2014-03-21
Dear Experts:

this nice macro, courtesy by rorya from EE, inserts graphics into Column D, linking them. The network paths for all these graphics (hyperlinks) are all located in Column C.

This great macro works just fine.

Could somebody help me tweak this code with the following requirements:

Line 15: The column letter should not be hard coded (that was my initial requirement) but dynamic, i.e. line 15 should be based on the column of the currently selected cell, ie. the column letter is to be dynamic.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub InsertPictures_Linked_To_File()

' by rorya from EE

   Dim C                           As Range
   Dim Image                       As Picture
   
 If MsgBox("Would you like to add pictures to the active worksheet, linking them?" & vbCrLf & vbCrLf & _
 "There must be network paths as hyperlinks to draw the picture from!", vbQuestion + vbYesNo, "Insert Pictures into Column D") = vbNo Then
        Exit Sub
        End If

  On Error Resume Next
   
   For Each C In Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
      C.Offset(0, 1).Activate
            Set Image = ActiveSheet.Pictures.Insert(C.Value2) 'linktofile:=msoFalse
      With Image
         If .Height > Application.CentimetersToPoints(4) Then _
                  .ShapeRange.ScaleHeight Application.CentimetersToPoints(4) / .Height, msoCTrue
                  .TopLeftCell.RowHeight = Image.Height + 10

         If .Height > .Width Then
            With .ShapeRange
               .Rotation = 90
               .IncrementLeft .Height / 2 - .Width / 2
               .IncrementTop .Width / 2 - .Height / 2 + 5
            End With
        .TopLeftCell.RowHeight = Image.Width + 10
        Else: .ShapeRange.IncrementTop (5)
         End If
      End With
   Next
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 39939059
Change:

   For Each C In Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))

To:

   For Each C In Range(ActiveCell.EntireColumn.Rows(2), ActiveCell.EntireColumn.Rows(Rows.Count).End(xlUp))

Kevin
0
 

Author Comment

by:AndreasHermle
ID: 39939164
Hi Kevin,

thank you very much for your quick help. I will give it a try and let you know.

Regards, andreas
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39939270
Purely for information (no points, please) you can also use Cells:

For Each C In Range(Cells(2, Activecell.Column), Cells(Rows.Count, Activecell.Column).End(xlUp))

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:senthilkumarsb
ID: 39939461
You can use named range for this.

for ex:- Create a named range for "C2" as MyCell

to get
value - ThisWorkbook.Names("MyCell").RefersToRange.Value
column - ThisWorkbook.Names("MyCell").RefersToRange.Column
row - ThisWorkbook.Names("MyCell").RefersToRange.Row

to get value
num = SheetName.Cells(i, Nms("MyCell").RefersToRange.Column).Value

to assign value
SheetName.Cells(i, Nms("MyCell").RefersToRange.Column).Value = 10
0
 

Author Closing Comment

by:AndreasHermle
ID: 39939605
Works great, exactly what I was looking for. Thank you very much.

Rory, thank you to you as well, I really appreciate it.

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
ID: 39944644
To: senthilkumarsb

Hi, thank you very much for your great support. I will give it a try and then let you know.

Regards, Andreas
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question