简体   繁体   English

(Excel VBA)-从宏中的单元格文本绘制

[英](Excel VBA) - Draw from cell text in a macro

I'm trying to build a small macro that allows the user to format multiple different documents at once. 我正在尝试构建一个小的宏,该宏允许用户一次格式化多个不同的文档。

I would like for the user to be able to enter into a particular cell within the document containing the macro a particular piece of text. 我希望用户能够在包含宏的文档中的特定单元格中输入特定文本。

I then want for this piece of text to be able to be drawn upon in the macro while affecting a different document. 然后,我希望在影响另一个文档的同时,可以在宏中绘制此文本。

For instance, a code to add another column might say 例如,添加另一列的代码可能会说

Worksheets(1).Range("A1").EntireColumn.Insert

Instead of specifying the column (A), I would like it to draw on a value in the host document. 我不想指定列(A),而是希望它使用宿主文档中的值。 For instance, the user types "G" into the particular cell, and then clicks a button to run the macro, and the macro will dynamically know to affect column G in all excel documents it targets based off of the value in the host document. 例如,用户在特定的单元格中键入“ G”,然后单击一个按钮以运行该宏,该宏将根据宿主文档中的值动态地知道要影响其定位的所有excel文档中的G列。

I hope this makes sense. 我希望这是有道理的。

Any suggestions for the sort of functions I should be looking at to make this work? 对于实现这项功能我应该考虑的各种功能有什么建议吗?

"Any suggestions on the sort of functions I should be looking at?" “对我应该考虑的功能有什么建议吗?” Here's a few... 几个...

To get the value which is entered... 要获取输入的值...

If the cell will always be in the same address, say A1: 如果单元格始终位于同一地址,请说A1:

' Define a string variable and set it equal to value in A1
Dim cellText as String
cellText = ThisWorkbook.ActiveSheet.Range("A1").Value

or instead of using Range you can also use Cells which takes a row and column number. 或者代替使用Range ,也可以使用带有行和列号的Cells

cellText = ThisWorkbook.ActiveSheet.Cells(1, 1).Value

If the cell changes then you may need to look into the Find function to look for a label/heading next to the input cell. 如果单元格发生更改,则可能需要查看“ Find功能以在输入单元格旁边Find标签/标题。 Then you can use it (easily with Cells ) to reference the input... 然后,您可以使用它(轻松使用Cells )来引用输入...

Once you have this variable, you can do what you like with it. 有了此变量后,就可以使用它进行操作。

To put this value into cell B3 in another (open) workbook named "MyWorkbook", and a sheet named "MySheet" you can do: 要将此值放入另一个(打开的)工作簿“ MyWorkbook”和名为“ MySheet”的工作表的单元格B3中,可以执行以下操作:

Application.Workbooks("MyWorkbook").Sheets("MySheet").Range("B3").Value = cellText

To insert a column at cellText , do 要在cellText处插入一列,请执行

Application.Workbooks("MyWorkbook").Sheets("MySheet").Range(cellText & "1").EntireColumn.Insert

Notably here, the & concatonates the strings together, so if 值得注意的是, &将字符串组合在一起,因此,如果

cellText="B"

then 然后

cellText & "1" = "B1"

Further to your comment about moving values between sheets, see my first example here, but always refer to the same workbook. 关于您关于在工作表之间移动值的评论,请参阅我的第一个示例,但始终参考同一工作簿。 If you find yourself repeatedly typing something like ThisWorkbook.Sheets("MySheet").<other stuff> 如果您发现自己反复键入类似ThisWorkbook.Sheets("MySheet").<other stuff>

then you can use the With shorthand. 那么您可以使用With速记。

With ThisWorkbook.Sheets("MySheet")

    ' Starting anything with a dot "." now assumes the with statement first
    .Range("A1").Value = .Range("A2").Value
    .Range("B1").Value = .Range("B2").Value

End With

Important to note is that this code has no data validation to check the cell's value before using it! 需要注意的重要一点是,此代码在使用之前没有数据验证来检查单元格的值! Simply trying to insert a column based on a value which could be anything is sure to make the macro crash within its first real world use! 简单地尝试根据可能是任何值的值插入一列,肯定会使宏在其第一次实际使用中崩溃!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM