每当他单击按钮时,我想向用户显示存在于单独工作表上的某些信息。
我可以在范围的起始行将 Excel 设置为“转到”此工作表,但我找不到隐藏其他所有内容的方法。
有什么方法可以解决这个问题,还是我必须隐藏所有行和列?
在工作簿的 VB 项目中插入一个用户窗体。
将 ListBox 控件添加到用户窗体。
UserForm_Activate
然后在事件代码中执行类似此代码的操作:
Private Sub UserForm_Activate()
Dim tbl As Range
Set tbl = Range("B2:E7") '## Change this to capture the rang you need '
Me.Caption = "Displaying data from " & _
ActiveSheet.Name & "!" & tbl.Address
With ListBox1
.ColumnHeads = False
.ColumnCount = tbl.Columns.Count
.RowSource = tbl.Address
End With
End Sub
它给出了范围内的未格式化数据:
要将范围导出为图像,您可以在用户窗体中创建图像而不是列表框。那么这应该足以让你开始。
正如您从这个屏幕截图中看到的那样,图像可能并不总是很清晰。此外,如果您正在使用大量单元格,则图像可能不适合您的用户表单等。我将把这部分留给您:)
Private Sub UserForm_Activate()
Dim tbl As Range
Dim imgPath As String
Set tbl = Range("B2:E7") '## Change this to capture the rang you need '
imgPath = Export_Range_Images(tbl)
Caption = "Displaying data from " & _
ActiveSheet.Name & "!" & tbl.Address
With Image1
If Not imgPath = vbNullString Then
.Picture = LoadPicture(imgPath)
.PictureSizeMode = fmPictureSizeModeClip
.PictureAlignment = 2 'Center
.PictureTiling = False
.SpecialEffect = 2 'Sunken
End If
End With
End Sub
Function Export_Range_Images(rng As Range) As String
'## Modified by David Zemens with
' credit to: _
' http://vbadud.blogspot.com/2010/06/how-to-save-excel-range-as-image-using.html ##'
Dim ocht As Object
Dim srs As Series
rng.CopyPicture xlScreen, xlPicture
ActiveSheet.Paste
Set ocht = ActiveSheet.Shapes.AddChart
For Each srs In ocht.Chart.SeriesCollection
srs.Delete
Next
'## Modify this line as needed ##'
fname = "C:\users\david_zemens\desktop\picture.jpg"
On Error Resume Next
Kill fname
On Error GoTo 0
ocht.Width = rng.Width
ocht.Height = rng.Height
ocht.Chart.Paste
ocht.Chart.Export Filename:=fname, FilterName:="JPG"
Application.DisplayAlerts = False
ocht.Delete
Application.DisplayAlerts = True
Set ocht = Nothing
Export_Range_Images = fname
End Function
如果您record a macro
手动隐藏一些列和行,将为您生成代码,您将看到它是如何完成的。