2016-11-28 50 views
1

我目前在Excel工作簿编程略有VBA代码自动显示在日志Excel界面。所以我在我的工作簿中有两张纸。其中一个显示当前日志,按紧急情况排序。另一个显示给定部门的新日志。工作簿连接到我们的日志数据库,并通过SQL数据库上的视图每5分钟刷新一次数据。我的问题是,目前,我确实将Height行设置为适合我的屏幕。我需要确保行高度被动态设置为始终显示相同数量的行。例如,如果用户想要看到5行,我希望他能够(通过我创建的表单)。然后我会使用5行滚动。所以这里是我的代码:适合X行使用VBA

'Check how many "Urgent" Logs are showing 
Dim XRange As Range 
    Set XRange = Cells(Rows.Count, 4).End(xlUp) 
    Set XRange = Range(Range("C1"), XRange) 
    AnswerUrgent = Application.CountIf(XRange, "Urgent") 

'Check how many "High" Logs are showing 
Dim YRange As Range 
    Set YRange = Cells(Rows.Count, 4).End(xlUp) 
    Set YRange = Range(Range("C1"), YRange) 
    AnswerHigh = Application.CountIf(YRange, "High") 

'Check how many logs to show 
    TotalAns = AnswerUrgent + AnswerHigh 

    For X = 1 To (Round(TotalAns/4) + 1) 
    If X > 1 Then 
     ActiveWindow.SmallScroll Down:=ScrollBy '(move down by 4 rows) 
    End If 

'Responsive sleep is to sleep and do events after each 250MS so the form will be 
'accessible 
' *4 to get 1 sec 
    ResponsiveSleep (DelayInSec * 4) 
    Next X 

我想要做的是使这种动态。如果用户选择5行显示,我希望能够通过5

向他们展示和滚动的5我不想虽然规模为全片。我的意思是我不想使用缩放来获取整张图片以适合我的屏幕。

谢谢!

+0

,我不认为有修改在Excel本身条的滚动本机的方式,将无助于使用户窗体滚动条? – Sgdva

+0

目标是让自己滚动。这是一个'仅查看'屏幕,将显示日志。 – InfiniteLoop

+0

有没有办法获得最大可显示高度?无论是在绝对或像素?这可以帮助我找到可显示的总行数(高度/我想要的行数)。 – InfiniteLoop

回答

0

我发现做到这一点的方式......我决定找到一种方法去全屏幕(没有标题栏和一切),所以我可以使用下面的:

Sub FormatWindow(ScrollBy) 



    Dim Xres As Integer 
    Dim Yres As Integer 
    Dim MaxXPoint As Single 
    Dim MaxYpoint As Single 
    Dim RowHeightForFour As Integer 



Xres = GetSystemMetrics(SM_CXSCREEN) 
Yres = GetSystemMetrics(SM_CYSCREEN) 


'1 pixel = 0.75 Point 
MaxXPoint = (Xres * 0.75) 
MaxYpoint = (Yres * 0.75) 

'Set the first row to 5% of screen 
PointForFirstRow = ((MaxYpoint * 5)/100) 

'Get the rest of the space to display rows 
MaxPointAfterFirstRow = MaxYpoint - ((MaxYpoint * 5)/100) 


'Set the first row to take 5% of the total displayable Y resolution 
ActiveSheet.Rows(1).RowHeight = PointForFirstRow 

'Get how many points per row 
PointPerRow = MaxPointAfterFirstRow/ScrollBy 

      Dim ws As Worksheet 
      For Each ws In Worksheets 

'If the sheet's name is not "Start", Apply the Height to all the rows. 
      If Not (ws.Name = "Start") Then 
      ws.Range("A2:A" & ws.Rows.Count).RowHeight = PointPerRow 
      End If 

      Next ws 

End Sub 

希望这有助于其他人!